﻿<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of correction_letters_controller
 *
 * @author wilson
 */
class CorrectionLettersController extends AppController {

    var $name = "CorrectionLetters";

    function index($claim_id = null) {
        $this->layout = "ajax";
        if (is_null($claim_id)) {
            $claim_id = 0;
        }
        $this->set('claim_id', $claim_id);
        $id = $this->Session->read('proy_id');
        $cod = $this->Session->read('cod');
        $this->set('cod', $cod);
        $this->set('id', $id);
        if ($id == "") {
            $this->Session->setFlash('No ha seleccionado Proyecto');
            $this->redirect(array('controller' => 'Proyects', 'action' => 'index'));
        } else {
            $this->layout = "ajax";
            if (empty($this->data)) {
                $this->CorrectionLetter->recursive = -1;
                $cartas = $this->CorrectionLetter->find('first', array('conditions' => array('CorrectionLetter.proyect_id' => $id, 'CorrectionLetter.claim_id' => $claim_id)));

                $this->set('cartas', $cartas);
                $idcarta = $cartas['CorrectionLetter']['id'];
                if ($idcarta != 0) {
                    $this->CorrectionLetter->ProyectRequirement->updateAll(array('ProyectRequirement.correction_letter_id' => $idcarta), array('ProyectRequirement.proyect_id' => $id, 'ProyectRequirement.calificacion' => 3, 'ProyectRequirement.claim_id' => $claim_id));
                    $this->CorrectionLetter->CandidateRequirement->updateAll(array('CandidateRequirement.correction_letter_id' => $idcarta), array('Candidate.proyect_id' => $id, 'CandidateRequirement.calificacion' => 3, 'CandidateRequirement.claim_id' => $claim_id));
                    $this->CorrectionLetter->PropertyRequirement->updateAll(array('PropertyRequirement.correction_letter_id' => $idcarta), array('Property.proyect_id' => $id, 'PropertyRequirement.calificacion' => 3, 'PropertyRequirement.claim_id' => $claim_id));
                    $this->CorrectionLetter->TitleStudy->updateAll(array('TitleStudy.correction_letter_id' => $idcarta), array('Property.proyect_id' => $id, 'TitleStudy.calificacion' => 3));
                    $requerimientos = $this->CorrectionLetter->query("SELECT ProyectRequirement.`id` ,Requirement.texto, ProyectRequirement.`cal_subsanacion` FROM `proyect_requirements` ProyectRequirement LEFT JOIN requirements Requirement ON ProyectRequirement.`requirement_id` = Requirement.id WHERE ProyectRequirement.proyect_id=$id AND ProyectRequirement.calificacion=3 AND ProyectRequirement.correction_letter_id=$idcarta and ProyectRequirement.claim_id=$claim_id");
                    $this->set('requisitos', $requerimientos);
                    $requerimientos = $this->CorrectionLetter->query(
                            "SELECT Candidate.nro_ident, Candidate.1er_nombre, Candidate.1er_apellido, CandidateRequirement.`id` , Requirement.texto, CandidateRequirement.`cal_subsanacion`
                           FROM `candidate_requirements` CandidateRequirement
                           LEFT JOIN requirements Requirement ON CandidateRequirement.`requirement_id` = Requirement.id
                           LEFT JOIN candidates Candidate ON CandidateRequirement.candidate_id = Candidate.id 
                           WHERE CandidateRequirement.calificacion =3 AND Candidate.proyect_id=$id AND CandidateRequirement.correction_letter_id=$idcarta AND CandidateRequirement.claim_id=$claim_id");
                    $this->set('requisitos1', $requerimientos);
                    $requerimientos = $this->CorrectionLetter->query(
                            "SELECT Property.matricula,Property.codigo,PropertyRequirement.`id` ,Requirement.texto, PropertyRequirement.`cal_subsanacion` FROM `property_requirements` PropertyRequirement LEFT JOIN requirements Requirement ON PropertyRequirement.`requirement_id` = Requirement.id LEFT JOIN properties Property on PropertyRequirement.property_id=Property.id  WHERE PropertyRequirement.calificacion=3 AND Property.proyect_id=$id AND PropertyRequirement.correction_letter_id=$idcarta AND PropertyRequirement.claim_id=$claim_id"
                    );
                    $this->set('requisitos2', $requerimientos);
                    $this->set('studies', $this->CorrectionLetter->TitleStudy->find('all', array('conditions' => array("TitleStudy.correction_letter_id" => $idcarta), 'fields' => array('TitleStudy.id', 'Property.id', 'Property.codigo', 'TitleStudy.concepto', 'TitleStudy.doc_subsanacion', 'TitleStudy.recomendaciones', 'TitleStudy.cal_subsanacion'), 'order' => array('TitleStudy.id ASC'))));
                }
            }
        }
    }

    function add($claim_id) {
        if (is_null($claim_id)) {
            $claim_id = 0;
        }
        $this->set('est', $this->Session->read('estado'));
        $this->layout = "ajax";
        $proyect_id = $this->Session->read('proy_id');
        $cod = $this->Session->read('cod');
        $this->set('cod', $cod);
        $this->set('id', $proyect_id);
        $this->set('claim_id', $claim_id);
        //echo $claim_id."FFFFF"; 
        if (!empty($this->data)) {
            $this->CorrectionLetter->create();
            if ($this->CorrectionLetter->save($this->data)) {
                $cid = $this->CorrectionLetter->id;
                $this->CorrectionLetter->ProyectRequirement->updateAll(array('ProyectRequirement.correction_letter_id' => $cid), array('ProyectRequirement.proyect_id' => $proyect_id, 'ProyectRequirement.calificacion' => 3, 'ProyectRequirement.claim_id' => $claim_id));
                $this->CorrectionLetter->CandidateRequirement->updateAll(array('CandidateRequirement.correction_letter_id' => $cid), array('Candidate.proyect_id' => $proyect_id, 'CandidateRequirement.calificacion' => 3, 'CandidateRequirement.claim_id' => $claim_id));
                $this->CorrectionLetter->PropertyRequirement->updateAll(array('PropertyRequirement.correction_letter_id' => $cid), array('Property.proyect_id' => $proyect_id, 'PropertyRequirement.calificacion' => 3, 'PropertyRequirement.claim_id' => $claim_id));
                App::Import('model', 'Property');

                $Property = new Property();
                $Property->recursive = -1;
                $predios = $Property->find('all', array('conditions' => array('Property.proyect_id' => $proyect_id), 'fields' => array('Property.id')));
                //$Property->TitleStudy->recursive = -1;
                foreach ($predios as $predio) {
                    $estudio = $Property->TitleStudy->find('first', array('conditions' => array('TitleStudy.property_id' => $predio['Property']['id']), 'fields' => array('TitleStudy.calificacion', 'TitleStudy.id'), 'order' => array('TitleStudy.id DESC')));
                    if ($estudio['TitleStudy']['calificacion']==3) {
                        $this->CorrectionLetter->TitleStudy->updateAll(array('TitleStudy.correction_letter_id' => $cid), array( 'TitleStudy.id' => $estudio['TitleStudy']['id']));
                
                    }
                }
                $this->Session->setFlash('Registro guardado con éxito');
                $this->redirect(array('controller' => 'CorrectionLetters', 'action' => 'index', $claim_id));
            }
        }
    }

    function edit($id) {
        $this->set('est', $this->Session->read('estado'));
        $cod = $this->Session->read('cod');
        $this->set('cod', $cod);
        $idp = $this->Session->read('proy_id');
        if (empty($this->data)) {
            $this->CorrectionLetter->recursive = -1;
            //$this->data = $this->CorrectionLetter->findById($id);
            $this->data = $this->CorrectionLetter->find("first", array("conditions" => array("CorrectionLetter.id" => $id)));
        } else {
            if ($this->CorrectionLetter->save($this->data)) {
                $cid = $this->data['CorrectionLetter']['id'];
                $this->CorrectionLetter->ProyectRequirement->updateAll(array('ProyectRequirement.correction_letter_id' => $cid), array('ProyectRequirement.proyect_id' => $idp, 'ProyectRequirement.calificacion' => 3));
                $this->CorrectionLetter->CandidateRequirement->updateAll(array('CandidateRequirement.correction_letter_id' => $cid), array('Candidate.proyect_id' => $idp, 'CandidateRequirement.calificacion' => 3));
                $this->CorrectionLetter->PropertyRequirement->updateAll(array('PropertyRequirement.correction_letter_id' => $cid), array('Property.proyect_id' => $idp, 'PropertyRequirement.calificacion' => 3));
                $this->CorrectionLetter->TitleStudy->updateAll(array('TitleStudy.correction_letter_id' => $cid), array('Property.proyect_id' => $idp, 'TitleStudy.calificacion' => 3));
                $this->Session->setFlash('Registro guardado con éxito');
                $this->redirect(array('controller' => 'CorrectionLetters', 'action' => 'index'));
            }
        }
    }

}
?>
